package com.imooc.handler;

import com.imooc.domain.Result;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author songjun
 * @date 2019/7/24.
 */
@ControllerAdvice
@Slf4j
public class BizExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result bizExceptionHandler(Exception e) {
        Result result = new Result();
        if (e instanceof BizException) {
            BizException bizException = (BizException) e;
            result.setCode(bizException.getCode());
            result.setMsg(bizException.getMessage());
        } else {
            log.error("系统异常-{}", e);
            result.setCode(ResultEnum.UNKNOW_ERROR.getCode());
            result.setMsg(ResultEnum.UNKNOW_ERROR.getMsg());
        }
        return result;
    }
}
